package cn.dylan.user.controller;

import cn.dylan.basic.exception.BusinessException;
import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.PageList;
import cn.dylan.user.domain.User;
import cn.dylan.user.dto.RegisterDto;
import cn.dylan.user.query.UserQuery;
import cn.dylan.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/user")
@Api(value = "用户的API",description="用户相关的CRUD功能")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * http://localhost:8080/User  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有用户信息" )
    public List<User> findAll(){

        return userService.loadAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/User/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public User findOne(@PathVariable("id") Long id){
        return userService.LoadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            userService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/User
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updateOrAdd(@RequestBody User User){

        try {
            if(User.getId()==null){
                // 添加操作
                userService.save(User);
            }else{
                // 修改操作
                userService.update(User);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query){
        try {
            // 哈哈哈
            return userService.queryPage(query);
            // ....
            // .....
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<User>();
        }
    }


    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            userService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }


    /**
     * 手机验证码注册接口
     * @param dto
     * @return
     */
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            userService.phoneRegister(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常，稍后重试！");
        }
    }


}























